//
//  KMethodViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/7.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "KMethodViewController.h"
#import "FontSizeSingleTon.h"
#import "FitHeader.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "KMethodModel.h"
#import <UIImageView+WebCache.h>
#import <MBProgressHUD.h>
#import "KMethodTableCell.h"
#define FontSize [[FontSizeSingleTon shareFontSize]array]
@interface KMethodViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableview;
@property(nonatomic, strong)NSMutableArray *dataArray;
@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UIImageView *methodImage;
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *descLabel;
@property(nonatomic, strong)UIImageView *hardImage;
@property(nonatomic, strong)UIImageView *timeImage;
@property(nonatomic, strong)UIImageView *tasteImage;
@property(nonatomic, strong)UILabel *hardLabel;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)UILabel *tasteLabel;
@property(nonatomic, strong)MBProgressHUD *progressView;
@end

@implementation KMethodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = YES;
    _progressView = [[MBProgressHUD alloc]initWithView:self.view];
    _progressView.labelText = @"加载中...";
    [_progressView setMode:MBProgressHUDModeIndeterminate];
    [_progressView show:YES];
    [self.view addSubview:_progressView];
    // Do any additional setup after loading the view.
}
-(void)loadView
{
    [super loadView];
    _dataArray = [NSMutableArray array];
    [self getData];
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT)];
    _tableview.bounces = NO;//关闭回弹
    [self creatHeaderViews];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    
    
    [self.view addSubview:_tableview];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataArray.count != 0) {
        KMethodModel *model = [[KMethodModel alloc]init];
        model = _dataArray[0];
        NSLog(@"%ld",model.step.count);
        return model.step.count;
    }
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 260;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    KMethodTableCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[KMethodTableCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        
    }
    cell.stepLabel.text = [[[_dataArray[0] step]objectAtIndex:indexPath.row] objectForKey:@"dishes_step_desc"];
    [cell.stepImage sd_setImageWithURL:[NSURL URLWithString:[[[_dataArray[0] step]objectAtIndex:indexPath.row] objectForKey:@"dishes_step_image"]] placeholderImage:[UIImage imageNamed:@""]];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
-(void)getData
{
       NSString *postUrlString = @"http://api.izhangchu.com/";
    NSString *bodyString = [NSString stringWithFormat:@"methodName=DishesView&dishes_id=%@&version=4.02",_methodID];
    [HTTPMethod getDataByString:postUrlString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        NSDictionary *dataDic = [data objectForKey:@"data"];
        KMethodModel *model = [[KMethodModel alloc]init];
        [model setValuesForKeysWithDictionary:dataDic];
        [_dataArray addObject:model];
        [_tableview reloadData];
        
        __block KMethodViewController *kdc = self;
        [_methodImage sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@""] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            _methodImage.image = [kdc getImageByImage:image];
            
        }];//调用截取部分图片方法;
        
        _nameLabel.text = model.dashes_name;
        _descLabel.text = model.material_desc;
        _hardLabel.text = [NSString stringWithFormat:@"难度 : %@",model.hard_level];
        _timeLabel.text = [NSString stringWithFormat:@"烹饪时间 : %@",model.cooke_time];
        _tasteLabel.text = [NSString stringWithFormat:@"口味 : %@",model.taste];
    }];
    [_progressView removeFromSuperview];
}
#pragma mark 截取部分图片方法
-(UIImage *)getImageByImage:(UIImage *)image
{
        CGRect rect = CGRectMake(0, image.size.height / 4.0, image.size.width, image.size.height / 4.0 * 2);
        CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage],rect);
        UIImage *imageResult = [UIImage imageWithCGImage:imageRef];
    return imageResult;
}
-(void)creatHeaderViews
{
    _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 364 *ht)];
    _methodImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 200 * ht)];
     [_headerView addSubview:_methodImage];
    _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake (20 * wt,_methodImage.frame.origin.y + _methodImage.frame.size.height + 20 * ht, 150 * wt, 20 * ht)];
    _nameLabel.font = [UIFont systemFontOfSize:[FontSize[0] integerValue]];
    _descLabel = [[UILabel alloc]initWithFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y + _nameLabel.frame.size.height + 10 * ht, 335 * wt, 50 * ht)];
    _descLabel.numberOfLines = 0;
    _descLabel.textColor = [UIColor colorWithRed:0.29 green:0.29 blue:0.29 alpha:1];
    _descLabel.font = [UIFont systemFontOfSize:[FontSize[2] integerValue]];
    _hardImage = [[UIImageView alloc]initWithFrame:CGRectMake(_descLabel.frame.origin.x + 20 * wt, _descLabel.frame.origin.y + _descLabel.frame.size.height + 5 * ht, 32 * wt, 32 * ht)];
    _hardImage.image = [UIImage imageNamed:@"nandu1@2x"];
    _hardLabel = [[UILabel alloc]initWithFrame:CGRectMake(_descLabel.frame.origin.x, _hardImage.frame.origin.y + _hardImage.frame.size.height + 5 * ht, 70 * wt, 20 * ht)];
    _hardLabel.textAlignment = 1;
    _hardLabel.font = [UIFont systemFontOfSize:[FontSize[1] integerValue]];
    
    _timeImage = [[UIImageView alloc]initWithFrame:CGRectMake(171.5 * wt,_hardImage.frame.origin.y, 32 * wt, 32 * ht)];
    _timeImage.image = [UIImage imageNamed:@"shijian@2x"];
    
    _timeLabel =[[UILabel alloc]initWithFrame:CGRectMake(127.5 * wt, _hardLabel.frame.origin.y, 120 * wt, 20 * ht)];
    _timeLabel.textAlignment = 1;
    _timeLabel.font = [UIFont systemFontOfSize:[FontSize[1] integerValue]];
    
    _tasteImage = [[UIImageView alloc]initWithFrame:CGRectMake(303 * wt,_hardImage.frame.origin.y, 32 * wt, 32 * ht)];
    _tasteImage.image = [UIImage imageNamed:@"kouwei@2x"];
    _tasteLabel = [[UILabel alloc]initWithFrame:CGRectMake(_tasteImage.frame.origin.x - 20 * wt, _hardLabel.frame.origin.y, 70 * wt, 20 * ht)];
    _tasteLabel.font = [UIFont systemFontOfSize:[FontSize[1] integerValue]];
    _tasteLabel.textAlignment = 1;
    [_headerView addSubview:_nameLabel];
    [_headerView addSubview:_descLabel];
    [_headerView addSubview:_hardImage];
    [_headerView addSubview:_hardLabel];
    [_headerView addSubview:_timeImage];
    [_headerView addSubview:_timeLabel];
    [_headerView addSubview:_tasteLabel];
    [_headerView addSubview:_tasteImage];
    _tableview.tableHeaderView = _headerView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
